package hc.bre.rest.cmn

import org.apache.shiro.authc.AuthenticationException
import org.apache.shiro.authc.IncorrectCredentialsException
import org.apache.shiro.authz.AuthorizationException
import org.apache.shiro.authz.UnauthenticatedException
import org.slf4j.*
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@groovy.transform.CompileStatic
@ControllerAdvice
@ResponseBody
class ExceptionHandlerAdvice {
	private static final Logger _log = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);

	@ExceptionHandler(UnauthenticatedException.class)
	public ReturnResult UnauthenticatedExceptionHandler(UnauthenticatedException ex) {
		_log.info("{}\t{}",ex.getClass(),ex.message);
		return new ReturnResult(1,'未登录系统，请登陆后访问。')
	}

	@ExceptionHandler(AuthorizationException.class)
	public ReturnResult AuthorizationExceptionHandler(AuthorizationException ex) {
		_log.info("{}\t{}",ex.getClass(),ex.message);
		return new ReturnResult(2,'你没有相应的访问权限，访问此功能之前需要获得管理员授权。')
	}

	@ExceptionHandler(IncorrectCredentialsException)
	public ReturnResult IncorrectCredentialsExceptionHandler(IncorrectCredentialsException ex) {
		_log.info("{}\t{}",ex.getClass(),ex.message);
		return new ReturnResult(3,'登录失败，错误的登录名、密码， 或账户被停用。'+(ex.message?"详情信息:"+ex.message:""))
	}

	@ExceptionHandler([Exception.class, Throwable.class])
	public ReturnResult exception(Throwable ex) {
		_log.info("{}",ex);
		return new ReturnResult(99, '系统错误。' + (ex.message?"详情信息:"+ex.message:""));
	}
}
